package com.jckl.foodathon.services.location;

import java.util.List;

import org.restlet.ext.jackson.JacksonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jckl.foodathon.R;
import com.jckl.foodathon.common.constant.CommunicationConstants;
import com.jckl.foodathon.model.Coordinate;
import com.jckl.foodathon.model.Store;
import com.jckl.foodathon.model.StoresListWidget;
import com.jckl.foodathon.widget.StoresListAdapter;

public class NearbyStoreQuery {
	
	public void getNearbyStores(Activity activity, View view, Coordinate coordinate, List<StoresListWidget> storesResultWidget, StoresListAdapter adapter){
		
		PerformStoreQueryAsync async = new PerformStoreQueryAsync(activity, view, coordinate, storesResultWidget, adapter);
		async.execute();
		
	}
	
	private class PerformStoreQueryAsync extends AsyncTask<Void, Void, Boolean> {
	
		Activity activity;
		Coordinate coordinate;
		String storesJson;
		ListView storeResult;
		List<StoresListWidget> storesResultWidget;
		StoresListAdapter adapter;
		ProgressDialog progress;
		
		public PerformStoreQueryAsync(Activity activity, View view, Coordinate coordinate, List<StoresListWidget> storesResultWidget, StoresListAdapter adapter) {
			this.activity = activity;
			this.coordinate = coordinate;
			this.coordinate.setQueryCount(1);
			this.storeResult = (ListView) view.findViewById(R.id.store_result_list);
			this.storesResultWidget = storesResultWidget;
			this.adapter = adapter;
		}
		
		@Override
		protected void onPreExecute() {
			progress = new ProgressDialog(activity);
			progress.setTitle(activity.getResources().getString(R.string.loading_title));
			progress.setMessage(activity.getResources().getString(R.string.loading_message));
			progress.show();
		}
		
		@Override
		protected Boolean doInBackground(Void... nothing) {
			

		
			boolean storeQuerySuccess = false;
			
			try{
				ClientResource cr = new ClientResource(CommunicationConstants.SERVER_URI+CommunicationConstants.LOCATION_STORENEARBY);
				JacksonRepresentation<Coordinate> coord = new JacksonRepresentation<Coordinate>(coordinate);
				Representation rep = cr.put(coord);
				storesJson = rep.getText();
				storeQuerySuccess = true;
				
			} catch (Exception ex){
				Log.e(CommunicationConstants.APP_TAG, "Unable to get nearby stores");
				ex.printStackTrace();
			}
			
			return storeQuerySuccess;
		}
		
		@Override
		protected void onProgressUpdate(Void... nothing){
			
	    }
		
		@Override
		protected void onPostExecute(Boolean successExecution) {
			progress.dismiss();
			
			if(successExecution){
				Gson gson = new Gson();
				Store[] stores = gson.fromJson(storesJson, Store[].class);
				for(int i=0;i<stores.length;i++){
					Store store = stores[i];
					StoresListWidget storeResult = new StoresListWidget();
					storeResult.setStoreName(store.getName());
					storeResult.setStoreArea(store.getAddress_area());
					storesResultWidget.add(storeResult);
				}
				adapter = new StoresListAdapter(activity.getApplicationContext(), storesResultWidget);
				storeResult.setAdapter(adapter);
			}else{
				Toast.makeText(activity.getApplicationContext(), "Location Query Failed", Toast.LENGTH_SHORT).show();
			}
			
		}
	}

}
